<?php
class job_management extends Zcontroller{
	public function index() {  
		$this->load->model("m_job");
        $data["all_location"]=$this->m_job->get_all_locations();
        $this->load->template("templates/general","jobs/job_view", "Zuka",$data);
    }
    function goto_companies_view(){
    	$this->load->model("m_job");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/job_management/goto_companies_view/';
    	$total_company=$this->m_job->count_companies();
      	$config['total_rows'] = count($total_company->result());
      	$config['per_page'] = '10';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data["all_company"]=$this->m_job->get_company($config['per_page'],$this->uri->segment(3));
        $this->load->template("templates/general","jobs/companies/companies_view", "Zuka",$data);
    }
    function goto_addnew_company(){
    	$this->load->model("m_job");
    	//$this->load->library("calendar");
        $data =array();
        $data['error_message']='';
        $data['txtname']='';
        $data['txtdesc']='';
        $data['txtaddress']='';
        $data['txtphone']='';
        $data['txtfax']='';
        $data['txtemail']='';
        $data['txtcontact']='';
        $data['txtwebsite']='';
       // $data['all_calendar']= $this->calendar->generate();
        $this->load->template("templates/general","jobs/companies/addnew_company", "Zuka",$data);
    }
    function goto_update_company($id){
    	$this->load->model("m_job");
        $data["error_message"] ="";
        $data["company"]=$this->m_job->get_company_by_comid($id);
        $this->load->template("templates/general","jobs/companies/update_company", "Zuka",$data);
    }
   
 function save_new_company(){
    	$this->load->model("m_job");
    	$this->load->helper('email');
    	$this->load->library('image_lib');


        $data =array();
        $name=trim($_POST['txtname']);
        $desc=trim($_POST['txtdesc']);
        $address=trim($_POST['txtaddress']);
        $phone=trim($_POST['txtphone']);
        $fax=trim($_POST['txtfax']);
        $email=trim($_POST['txtemail']);
        $contact=trim($_POST['txtcontact']);
        $website=trim($_POST['txtwebsite']);
        $error_message = "";
        $company_name=$this->m_job->get_company_by_name($name);
        if ($name == "") 
        {
        	$error_message = "Company name is required.";	
        }elseif( $company_name<>'')
        {
    		$error_message="Company name already exists.";
    	}else
    	{
        	if ($email!='' && !valid_email($email)){
        		$error_message="Email is not valid.";
        	}
        }
        $img_type=$_FILES['imagefile']['type'];
        if(isset($img_type)){
        	$imgname=$_FILES['imagefile']['name'];
        	$tem_img=$_FILES['imagefile']['tmp_name'];
        	$imgsize=$_FILES['imagefile']['size'];
        	$img_content = file_get_contents($tem_img);
        	if($imgsize >= 200000){ // in byte
        		$error_message="Image must be less than 200kb.";
        	}
        	
        }else{
        	$imgname='';
       		$img_content = ''; 
        	$imgsize='';
        }
        if ($error_message != "") {
        	$data['txtname']=$name;
        	$data['txtdesc']=$desc;
	        $data['txtaddress']=$address;
	        $data['txtphone']=$phone;
	        $data['txtfax']=$fax;
	        $data['txtemail']=$email;
	        $data['txtcontact']=$contact;
	        $data['txtwebsite']=$website;
        	$data["error_message"] = $error_message;
        	$this->load->template("templates/general","jobs/companies/addnew_company", "Zuka",$data);
        	return;
        }
        
        $this->m_job->insert_new_company($name,
							        	$desc,
							        	$address,
							        	$phone,
							        	$fax,
							        	$email,
							        	$contact,
							        	$website,
							        	$img_content,
							        	$imgsize,
							        	$imgname,
							        	$img_type);
							        	
		//$this->m_job->upload_logo($logoname,$logotype,$comid['com_id']);
		redirect(site_url("job_management/goto_companies_view"));
    }

	function showcompanylogo($com_id){
           $this->load->model("m_job");
           $result=$this->m_job->get_company_by_comid($com_id);
           $image_content=pg_unescape_bytea($result['com_logo']);
           header("Content-type:".$result['com_logo_type']);
           print $image_content;
       }

    function save_update_company($comid){
    	$this->load->model("m_job");
    	$this->load->helper('email');
        $data =array();
        $name=trim($_POST['txtname']);
        $desc=trim($_POST['txtdesc']);
        $address=trim($_POST['txtaddress']);
        $phone=trim($_POST['txtphone']);
        $fax=trim($_POST['txtfax']);
        $email=trim($_POST['txtemail']);
        $contact=trim($_POST['txtcontact']);
        $website=trim($_POST['txtwebsite']);
        $error_message = "";
        $company_name = $this->m_job->get_company_by_name($name);
        if ($name == "") {
        	$error_message = "Company name is required.";	
        }elseif ($email!='' && !valid_email($email)){
        	$error_message="Email is not valid.";
        }else{
        	if($company_name['com_name']<>''){
        		if($company_name['com_id']!= $comid){
        			$error_message="Company name already exists.";
        		}
        	}
        }
    	$logotype=$_FILES['imagefile']['type'];
      $chkdeletelogo=$_POST['chkdelete'];
      if($chkdeletelogo==''){  //not delete image
         if($logotype<>''){     //when user choose another image
         	$imgname=$_FILES['imagefile']['name'];
	        $tem_img=$_FILES['imagefile']['tmp_name'];
	        $imgsize=$_FILES['imagefile']['size'];
	        $img_content = file_get_contents($tem_img);
        	if($imgsize >= 200000){ // in byte
        		$error_message="Image must be less than 200kb.";
        	}else{
        		$this->m_job->update_logo($img_content,$imgsize,$imgname,$logotype,$comid);
        	}
          }
      }else{   //when user check on checkbox to delete image
      		if($logotype<>''){ //check to delete n choose new image
      			$imgname=$_FILES['imagefile']['name'];
		        $tem_img=$_FILES['imagefile']['tmp_name'];
		        $imgsize=$_FILES['imagefile']['size'];
		        $img_content = file_get_contents($tem_img);
	      		if($imgsize >= 200000){ // in byte
	        		$error_message="Image must be less than 200kb.";
	        	}else{
	        		$this->m_job->update_logo($img_content,$imgsize,$imgname,$logotype,$comid);
	        	}

            }else{  //check on checkbox to delete image without choosing the new one
                $this->m_job->update_logo('0','0','','',$comid);
            }
      }
        if ($error_message != "") {
        	$data["error_message"] = $error_message;
        	$data["company"]=$this->m_job->get_company_by_comid($comid);
        	$this->load->template("templates/general","jobs/companies/update_company", "Zuka",$data);
        	return;
        }
        $this->m_job->update_company($name,
							         $desc,
							         $address,
							         $phone,
							         $fax,
							         $email,
							         $contact,
							         $website,
							         $comid);
     
      
		redirect(site_url("job_management/goto_companies_view"));
    }
 
    function delete_company($comid){
    	$this->load->model("m_job");
    	$data['announdata']=$this->m_job->count_announ_by_comid($comid);//get announ_id where com_id
    	foreach ($data['announdata']->result() as $announ)://get all announcement of company
    		$data['titledata']=$this->m_job->get_title_by_announ($announ->announ_id);
    		foreach ($data['titledata']->result() as $title)://get all title of announcement
    			$this->m_job->delete_titleloc_by_titleid($title->title_id);//table: titles_locations
    			$this->m_job->delete_requirement_by_titleid($title->title_id);//table: requirement
    			$this->m_job->delete_duty_by_titleid($title->title_id);//table: duty
    			$this->m_job->delete_titlecate_by_titleid($title->title_id);//table: titles_categories
    		endforeach;
    		$this->m_job->delete_title_annouid($announ->announ_id);//table: title
    		$this->m_job->delete_announ($announ->announ_id);//table: announcement
    	endforeach;
    	$this->m_job->delete_company($comid);
    	redirect(site_url("job_management/goto_companies_view"));
    }
    
    //========== announce ===========
    function goto_announce_view(){
    	$this->load->model("m_job");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/job_management/goto_announce_view/';
    	$total_announ=$this->m_job->count_announ();
      	$config['total_rows'] = count($total_announ->result());
      	$config['per_page'] = '10';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data["all_company"]=$this->m_job->count_companies();//get all companies name 
        $data["all_announce"]=$this->m_job->get_allannoun($config['per_page'],$this->uri->segment(3));
        $this->load->template("templates/general","jobs/job_announcements/announ_view", "Zuka",$data);
    }
    function goto_addnew_announ(){
    	$this->load->model("m_job");
    	$data =array();
        $data['error_message']='';
        $data['cbocompany']='';
        $data['datepost']='';
        $data['dateclose']='';
        $data['txtnote']='';
    	$data["all_company"]=$this->m_job->count_companies();
    	$this->load->template("templates/general","jobs/job_announcements/addnew_announ", "Zuka",$data);
    }
   
    function save_addnew_announ(){
    	$this->load->model("m_job");
    	$data =array();
    	date_default_timezone_set('Asia/Manila');
        $company=trim($_POST['cbocompany']);
        $postdate =$_POST['datepost'];
        $closedate=$_POST['dateclose'];
        $notice=trim($_POST['txtnote']);
        $error_message = "";
        $currentdate=date("m/d/Y");
        if ($postdate==""){
        	$error_message = "Post date is required.";
        }elseif (strtotime($postdate) < strtotime($currentdate)){
        	$error_message = "Post date must be today or later.";
        }elseif ($closedate==""){
        	$error_message = "Closing date is required."; 
        }elseif (strtotime($closedate) <= strtotime($postdate)){
        	$error_message = "Closing date must be greater than post date.";
        }else{	
        	if($notice==""){
        		$error_message = "Notice is required.";
        	}
        }
        if ($error_message != "") {
        	$data["all_company"]=$this->m_job->count_companies();
        	$data["cbocompany"]=$company;
        	$data["datepost"]=$postdate;
        	$data["dateclose"]=$closedate;
        	$data["error_message"] = $error_message;
        	$data["txtnote"]=$notice;
        	$this->load->template("templates/general","jobs/job_announcements/addnew_announ", "Zuka",$data);
        	return;
        }
        $this->m_job->save_new_announ($postdate,$closedate,$notice,$company);
		redirect(site_url("job_management/goto_announce_view"));
    }
    function goto_update_announ($comid,$announid){
    	$this->load->model("m_job");
    	$data["error_message"] ="";
        $data["all_company"]=$this->m_job->count_companies();
        $data["companyname"]=$this->m_job->get_company_by_comid($comid);
        $data["announce"]=$this->m_job->get_announ_by_id($announid);
        $this->load->template("templates/general","jobs/job_announcements/update_announ", "Zuka",$data);
    }
    function save_update_announ($announid){
    	$this->load->model("m_job");
    	date_default_timezone_set('Asia/Manila');
    	$data =array();
        $company=trim($_POST['cbocompany']);
    	$postdate =$_POST['datepost'];
        $closedate=$_POST['dateclose'];
        $notice=trim($_POST['txtnote']);
        $error_message = "";
        $currentdate=date("m/d/Y");
        if ($postdate==""){
        	$error_message = "Post date is required.";
        }elseif (strtotime($postdate) < strtotime($currentdate)){
        	$error_message = "Post date must be today or later.";
        }elseif ($closedate==""){
        	$error_message = "Closing date is required."; 
        }elseif (strtotime($closedate) <= strtotime($postdate)){
        	$error_message = "Closing date must be greater than post date.";
        }else{	
        	if($notice==""){
        		$error_message = "Notice is required.";
        	}
        }
        if ($error_message != "") {
        	$data["error_message"] = $error_message;
        	$data["all_company"]=$this->m_job->count_companies();
        	$data["announce"]=$this->m_job->get_announ_by_id($id);
        	$this->load->template("templates/general","jobs/job_announcements/update_announ", "Zuka",$data);
        	return;
        }
        $this->m_job->save_update_announ($postdate,$closedate,$notice,$company,$announid);
		redirect(site_url("job_management/goto_announce_view"));
    }
    function delete_announ($announid){
    	$this->load->model("m_job");
    	$data['titledata']=$this->m_job->get_title_by_announ($announid);
    	foreach ($data['titledata']->result() as $title)://get all title of announcement
    		$this->m_job->delete_titleloc_by_titleid($title->title_id);//table: titles_locations
    		$this->m_job->delete_requirement_by_titleid($title->title_id);//table: requirement
    		$this->m_job->delete_duty_by_titleid($title->title_id);//table: duty
    		$this->m_job->delete_titlecate_by_titleid($title->title_id);//table: titles_categories
    	endforeach;
    	$this->m_job->delete_title_annouid($announid);//table: title
    	$this->m_job->delete_hit_job($announid);//table: hit_jobs
    	$this->m_job->delete_announ($announid);//table: announcement
    	
		redirect(site_url("job_management/goto_announce_view"));
    }
    function search_result(){
    	$this->load->model("m_job");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$comid=trim($_POST['cbocompany']);
    	$data["com_id"]=$comid;
    	$config['base_url'] = base_url().'index.php/job_management/search_result/';
    	$total_announ=$this->m_job->count_announ_by_comid($comid);
      	$config['total_rows'] = count($total_announ->result());
      	$config['per_page'] = '10';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data["all_company"]=$this->m_job->count_companies();
        $data["com_announce"]=$this->m_job->get_announ_by_comid($comid,$config['per_page'],$this->uri->segment(3));
        $this->load->template("templates/general","jobs/job_announcements/search_result", "Zuka",$data);
    }
    //===================== title =============================
    function goto_title_view($comid,$announid){
    	$this->load->model("m_job");
    	$data["company"]=$this->m_job->get_company_by_comid($comid);
    	$data["announceid"]=$announid;
    	$data["all_title"]=$this->m_job->get_title_by_announ($announid);
    	$this->load->template("templates/general","jobs/job_announcements/title_view", "Zuka",$data);
    }
    function goto_addnew_title($comid,$announid){
    	$this->load->model("m_job");
    	$data =array();
        $data['error_message']='';
        $data['anid']=$announid;
        $company=$this->m_job->get_company_by_comid($comid);
        $data['txtcompany']=$company['com_name'];
        $data['comid']=$comid;
        $data['txtdesc']='';
    	$this->load->template("templates/general","jobs/job_announcements/addnew_title", "Zuka",$data);
    }
    function save_addnew_title($comid,$announid){
    	$this->load->model("m_job");
    	$data =array();
    	$description=trim($_POST['txtdesc']);
    	$comname=trim($_POST['txtcompany']);
        if ($description==""){
        	$data["error_message"] = "Description is required.";
        	$data['txtannounid']=$announid;
        	$data['txtcompany']=$comname;
        	$data['txtdesc']=$description;
    		$this->load->template("templates/general","jobs/job_announcements/addnew_title", "Zuka",$data);	
    		return ;
        }
        $this->m_job->insert_new_title($description,$announid);
        redirect(site_url("job_management/goto_title_view/".$comid."/".$announid));        
    }
    function goto_update_title($titleid,$comid){//goto_title_view($comid,$announid){
    	$this->load->model("m_job");
    	$data =array();
        $data['error_message']='';
        $company=$this->m_job->get_company_by_comid($comid);
        $data['companyname']=$company['com_name'];
        $data['companyid']=$comid;
        $data['announ']=$this->m_job->get_announ_by_titleid($titleid);
        $data['titles']=$this->m_job->get_title_by_id($titleid);
    	$this->load->template("templates/general","jobs/job_announcements/update_title", "Zuka",$data);
    }
    function save_update_title($comid,$announid){
    	$this->load->model("m_job");
    	$titleid=trim($_POST['txttitleid']);
    	$titledesc=trim($_POST['txttitledesc']);
    	if($titledesc==''){
    		$data['error_message']='Description is required.';
    		$company=$this->m_job->get_company_by_comid($comid);
	        $data['companyname']=$company['com_name'];
	        $data['companyid']=$comid;
	        $data['announ']=$this->m_job->get_announ_by_comid($comid);
	        $data['titles']=$this->m_job->get_title_by_id($titleid);
	    	$this->load->template("templates/general","jobs/job_announcements/update_title", "Zuka",$data);
    		return ;
    	}
    	$this->m_job->save_update_title($titledesc,$announid,$titleid);//save_update_title($desc,$annouid,$titleid)
        redirect(site_url("job_management/goto_title_view/".$comid."/".$announid));   
    }
    function delete_title($titleid,$comid){
    	$this->load->model("m_job");
    	$announ=$this->m_job->get_announ_by_titleid($titleid);
    	$this->m_job->delete_titleloc_by_titleid($titleid);//delete in table title_location
    	$this->m_job->delete_titlecate_by_titleid($titleid);//delete in table title_category
    	$this->m_job->delete_duty_by_titleid($titleid);//delete in table duties
    	$this->m_job->delete_requirement_by_titleid($titleid);//delete in table requirement
    	$this->m_job->delete_title($titleid);
		redirect(site_url("job_management/goto_title_view/".$comid."/".$announ['announ_id'])); 
    }
    
    //=================== Job Category ======================
    function goto_category_view(){
    	$this->load->model("m_job");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/job_management/goto_category_view/';
    	$total_category=$this->m_job->count_category();
      	$config['total_rows'] = count($total_category->result());
      	$config['per_page'] = '10';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
    	$data['all_cate']=$this->m_job->get_all_category($config['per_page'],$this->uri->segment(3));
    	$this->load->template("templates/general","jobs/job_categories/category_view", "Zuka",$data);
    }
    
    function goto_addnew_category(){
    	$this->load->model("m_job");
    	$data =array();
        $data['error_message']='';
        $data['txtcatedesc']='';
    	$this->load->template("templates/general","jobs/job_categories/addnew_category", "Zuka",$data);
    }
    function save_addnew_category(){
    	$this->load->model("m_job");
    	$catedesc=trim($_POST['txtcatedesc']);
    	$error_message='';
    	$category= $this->m_job->get_category_by_name($catedesc);
        if($catedesc==""){
        	$error_message="Description is required. ";
        }else{//check if category desc exists
        	if($category!=''){
        		$error_message="Description already exists.";
        	}
        }
        if($error_message!=''){
        	$data['error_message']=$error_message;
        	$data["txtcatedesc"]=$catedesc;
        	$this->load->template("templates/general","jobs/job_categories/addnew_category", "Zuka",$data);
        	return;
        }
        $this->m_job->insert_new_category($catedesc);
		redirect(site_url("job_management/goto_category_view"));
    }
    function goto_update_category($cateid){
    	$this->load->model("m_job");
    	$data =array();
        $data['error_message']='';
    	$data['cate']=$this->m_job->get_category_by_id($cateid);
    	$this->load->template("templates/general","jobs/job_categories/update_category", "Zuka",$data);
    }
    function save_update_category(){
    	$this->load->model("m_job");
    	$cateid=trim($_POST['txtcateid']);
    	$catedesc=trim($_POST['txtcatedesc']);
    	$data =array();
        $error_message='';
        $category= $this->m_job->iscategoryexist($cateid,$catedesc);
        if($catedesc==""){
        	$error_message="Description is required.";
        }else {
	       	if($category!=''){
	       		$error_message="Description already exists.";
	       	}
        }
        if($error_message!=''){
        	$data['error_message']=$error_message;
        	$data['cate']=$this->m_job->get_category_by_id($cateid);
    		$this->load->template("templates/general","jobs/job_categories/update_category", "Zuka",$data);
    		return ;
        }
        $this->m_job->save_update_category($catedesc,$cateid);
		redirect(site_url("job_management/goto_category_view"));
    }
    function delete_category($cateid){
    	$this->load->model("m_job");
    	$this->m_job->delete_category($cateid);
		redirect(site_url("job_management/goto_category_view"));
    }
    
    //================== Duty ============================
     function goto_duty_view($titleid){
    	$this->load->model("m_job");
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$data['all_duties']=$this->m_job->get_number_duty($titleid);
    	$data['announ']=$this->m_job->get_announ_by_titleid($titleid);
    	$data['company']=$this->m_job->get_company_by_announid($data['announ']['announ_id']);
    	$this->load->template("templates/general","jobs/job_announcements/duty/duty_view", "Zuka",$data);
    }
    function goto_addnew_duty($titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['txtdutydesc']='';
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$this->load->template("templates/general","jobs/job_announcements/duty/addnew_duty", "Zuka",$data);
    }
    function save_addnew_duty($titleid){
    	$this->load->model("m_job");
    	$dutydesc=trim($_POST['txtdutydesc']);
    	if($dutydesc==''){
    		$data['error_message']='Duty description is required.';
    		$data['txtdutydesc']=$dutydesc;
	    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$data['all_duty']=$this->m_job->get_number_duty($titleid);
	    	$this->load->template("templates/general","jobs/job_announcements/duty/addnew_duty", "Zuka",$data);
	    	return ;
    	}
    	$this->m_job->insert_new_duty($dutydesc,$titleid);
    	redirect(site_url("job_management/goto_duty_view/$titleid"));
    }
    function goto_update_duty($dutyid,$titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['duty']=$this->m_job->get_duty_by_id($dutyid);
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$this->load->template("templates/general","jobs/job_announcements/duty/update_duty", "Zuka",$data);
    }
    function save_update_duty($titleid){
    	$this->load->model("m_job");
    	$dutyid=trim($_POST['txtdutyid']);
    	$dutydesc=trim($_POST['txtdutydesc']);
    	if($dutydesc==''){
    		$data['error_message']='Duty description is required.';
    		$data['duty']=$this->m_job->get_duty_by_id($dutyid);
	    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$this->load->template("templates/general","jobs/job_announcements/duty/update_duty", "Zuka",$data);
	    	return ;
    	}
    	$this->m_job->save_update_duty($dutyid,$dutydesc,$titleid);
    	redirect(site_url("job_management/goto_duty_view/$titleid"));
    }
    function delete_duty($dutyid,$titleid){
    	$this->load->model("m_job");
    	$this->m_job->delete_duty($dutyid);
		redirect(site_url("job_management/goto_duty_view/$titleid"));
    }
    //================== Requirement =====================
	function goto_requirement_view($titleid){
    	$this->load->model("m_job");
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$data['all_requirement']=$this->m_job->get_number_requirement($titleid);
    	$data['announ']=$this->m_job->get_announ_by_titleid($titleid);
    	$data['company']=$this->m_job->get_company_by_announid($data['announ']['announ_id']);
    	$this->load->template("templates/general","jobs/job_announcements/requirement/requirement_view", "Zuka",$data);
    }
	function goto_addnew_requirement($titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['txtreqdesc']='';
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$this->load->template("templates/general","jobs/job_announcements/requirement/addnew_requirement", "Zuka",$data);
    }
    function save_addnew_requirement($titleid){
    	$this->load->model("m_job");
    	$reqid=trim($_POST['txtreqid']);
    	$reqdesc=trim($_POST['txtreqdesc']);
    	if($reqdesc==''){
    		$data['error_message']='Requirement description is required.';
    		$data['txtreqdesc']=$reqdesc;
	    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$this->load->template("templates/general","jobs/job_announcements/requirement/update_requirement", "Zuka",$data);
	    	return ;
    	}
    	$this->m_job->insert_new_requirement($reqdesc,$titleid);
    	redirect(site_url("job_management/goto_requirement_view/$titleid"));
    }
    function goto_update_requirement($reqid,$titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['Requirement']=$this->m_job->get_requirement_by_id($reqid);
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$this->load->template("templates/general","jobs/job_announcements/requirement/update_requirement", "Zuka",$data);
    }
    function save_update_requirement($titleid){
    	$this->load->model("m_job");
    	$reqid=trim($_POST['txtreqid']);
    	$reqdesc=trim($_POST['txtreqdesc']);
    	if($reqdesc==''){
    		$data['error_message']='Requirement description is required.';
    		$data['Requirement']=$this->m_job->get_requirement_by_id($reqid);
	    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$this->load->template("templates/general","jobs/job_announcements/requirement/update_requirement", "Zuka",$data);
	    	return ;
    	}
    	$this->m_job->save_update_requirement($reqid,$reqdesc,$titleid);
    	redirect(site_url("job_management/goto_requirement_view/$titleid"));
    }
    function delete_requirement($reqid,$titleid){
    	$this->load->model("m_job");
    	$this->m_job->delete_requirement($reqid);
		redirect(site_url("job_management/goto_requirement_view/$titleid"));
    }
    
    //==================== titles_categories =================
	function goto_view_category_titleid($titleid){
    	$this->load->model("m_job");
    	$data['title']=$this->m_job->get_title_by_id($titleid);
    	$data['all_cate']=$this->m_job->get_number_category($titleid);
    	$data['announ']=$this->m_job->get_announ_by_titleid($titleid);
    	$data['company']=$this->m_job->get_company_by_announid($data['announ']['announ_id']);
    	$this->load->template("templates/general","jobs/job_announcements/title_category/titlecate_view", "Zuka",$data);
    }
    function goto_addnew_titlecate($titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$data['all_category']=$this->m_job->get_data_category();
    	$this->load->template("templates/general","jobs/job_announcements/title_category/addnew_titlecate", "Zuka",$data);
    }
    function save_addnew_titlecate($titleid){
    	$this->load->model("m_job");
    	$cateid=trim($_POST['cbocategory']);
    	$istitlecateexist=$this->m_job->istitlecategoryexist($titleid,$cateid);
    	if($istitlecateexist!=''){
    		$data['error_message']="Category already exists.";
    		$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$data['all_category']=$this->m_job->get_all_category();
	    	$this->load->template("templates/general","jobs/job_announcements/title_category/addnew_titlecate", "Zuka",$data);
    		return ;
    	}
    	$this->m_job->save_addnew_titlecate($titleid,$cateid);
    	redirect(site_url("job_management/goto_view_category_titleid/$titleid"));
    }
    function delete_titlecate($cateid,$titleid){
    	$this->load->model("m_job");
    	$this->m_job->delete_titlecate_by_cateid($cateid);
    	redirect(site_url("job_management/goto_view_category_titleid/$titleid"));
    }
    
    //================== titles_locations ==========================
	function goto_view_location_titleid($titleid){
    	$this->load->model("m_job");
    	$data['title']=$this->m_job->get_title_by_id($titleid);
    	$data['all_location']=$this->m_job->get_number_location($titleid);
    	$data['announ']=$this->m_job->get_announ_by_titleid($titleid);
    	$data['company']=$this->m_job->get_company_by_announid($data['announ']['announ_id']);
    	$this->load->template("templates/general","jobs/job_announcements/title_location/titleloc_view", "Zuka",$data);
    }
    function goto_addnew_titleloc($titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$data['all_location']=$this->m_job->get_all_locations();
    	$this->load->template("templates/general","jobs/job_announcements/title_location/addnew_titleloc", "Zuka",$data);
    }
    function save_addnew_titleloc($titleid){
    	$this->load->model("m_job");
    	$locid=trim($_POST['cbolocation']);
    	$istitlelocexist=$this->m_job->istitlelocationexist($titleid,$locid);
    	if($istitlelocexist!=''){
    		$data['error_message']="Location already exists.";
    		$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$data['all_location']=$this->m_job->get_all_locations();
	    	$this->load->template("templates/general","jobs/job_announcements/title_location/addnew_titleloc", "Zuka",$data);
    		return ;
    	}
    	$this->m_job->save_addnew_titleloc($titleid,$locid);
    	redirect(site_url("job_management/goto_view_location_titleid/$titleid"));
    }
    function delete_titleloc($locid,$titleid){
    	$this->load->model("m_job");
    	$this->m_job->delete_titleloc_by_locid($locid);
    	redirect(site_url("job_management/goto_view_location_titleid/$titleid"));
    }
    
    
    //================================== titles_schedule ==================
	function goto_view_schedule_titleid($titleid){
    	$this->load->model("m_job");
    	$data['title']=$this->m_job->get_title_by_id($titleid);
    	$data['all_schedule']=$this->m_job->get_number_schedule($titleid);
    	$data['announ']=$this->m_job->get_announ_by_titleid($titleid);
    	$data['company']=$this->m_job->get_company_by_announid($data['announ']['announ_id']);
    	$this->load->template("templates/general","jobs/job_announcements/title_schedule/title_schedule_view", "Zuka",$data);
    }
 function goto_addnew_titleschedule($titleid){
    	$this->load->model("m_job");
    	$data['error_message']='';
    	$data['titledata']=$this->m_job->get_title_by_id($titleid);
    	$data['all_schedule']=$this->m_job->get_all_schedule();
    	$this->load->template("templates/general","jobs/job_announcements/title_schedule/addnew_title_schedule", "Zuka",$data);
    }
    function save_addnew_titleschedule($titleid){
    	$this->load->model("m_job");
    	$scheduleid=trim($_POST['cboschedule']);
    	$isscheduleexist=$this->m_job->istitlescheduleexist($titleid,$scheduleid);
    	if($isscheduleexist!=''){
    		$data['error_message']="Schedule already exists.";
    		$data['titledata']=$this->m_job->get_title_by_id($titleid);
	    	$data['all_schedule']=$this->m_job->get_all_schedule();
	    	$this->load->template("templates/general","jobs/job_announcements/title_schedule/addnew_title_schedule", "Zuka",$data);
    		return ;
    	}
    	$this->m_job->save_addnew_titleschedule($titleid,$scheduleid);
    	redirect(site_url("job_management/goto_view_schedule_titleid/$titleid"));
    }
    function delete_titleschedule($scheduleid,$titleid){
    	$this->load->model("m_job");
    	$this->m_job->delete_titleschedule_by_scheduleid($scheduleid);
    	redirect(site_url("job_management/goto_view_schedule_titleid/$titleid"));
    }
}
?>